package org.example.zuoye.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.zuoye.entity.Admin;
import org.example.zuoye.entity.Log;
import org.example.zuoye.mapper.AdminMapper;
import org.example.zuoye.mapper.LogMapper;
import org.example.zuoye.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author xinchen
 * @Date: 2024/08/20/ 17:35
 * @Description
 */
@Service
public class LogServiceImpl implements LogService {

    @Autowired
    private LogMapper logMapper;

    @Autowired
    private AdminMapper adminMapper;
    /**
     * 分页查询
     */
    @Override
    public PageInfo search(Integer page, Integer limit, Log log) {
        try(Page<?> __ = PageHelper.startPage(page, limit)) {
            return new PageInfo<>(logMapper.search(log));
        }
    }

    /**
     * 批量删除
     */
    @Override
    public int remove(Integer[] ids) {
        return logMapper.deleteByIds(ids);
    }

    /**
     *  新增操作
     */
    @Override
    public int add(Log log) throws Exception {
        Admin admin = adminMapper.selectByUserName(log.getUsername());
        if (ObjectUtils.isEmpty(log.getUsername())) {
            throw new Exception("用户名不能为空");
        }
        log.setUserId(admin.getId());
        // 获取当前时间
        Date currentTime = new Date();
        log.setPublishDate(currentTime);
        return logMapper.insert(log);
    }

    /**
     * 修改操作
     */
    @Override
    public int update(Log log) throws Exception  {

        // 获取当前时间
        Date currentTime = new Date();
        log.setPublishDate(currentTime);


        return logMapper.updateByPrimaryKey(log);
    }

    @Override
    public List findAll() {
        return logMapper.findAll();
    }

}
